﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FinalSilverlightProject.Views.Login
{
    public partial class NavigationBar : UserControl
    {
        public NavigationBar()
        {
            InitializeComponent();
            if (DesignerProperties.IsInDesignTool)
            {
                VisualStateManager.GoToState(this, "GlobalState", false);
            }
            else
            {
                //this.DataContext = WebContext.Current;
                WebContext.Current.Authentication.LoggedIn += AuthenticationLoggedIn;
                WebContext.Current.Authentication.LoggedOut += AuthenticationLoggedOut;
                UpdateLoginState();
            }
        }

        private void UpdateLoginState()
        {
            if (WebContext.Current.User.IsAuthenticated)
            {
                string currentRole = WebContext.Current.User.Roles.First();
                VisualStateManager.GoToState(this, currentRole + "LoggedIn", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "GlobalState", true);
            }

        }

        void AuthenticationLoggedOut(object sender, AuthenticationEventArgs e)
        {
            UpdateLoginState();
        }

        void AuthenticationLoggedIn(object sender, AuthenticationEventArgs e)
        {
            UpdateLoginState();
        }
    }
}
